Page({
  data: {
    currentTime: '12:30',
    currentFilter: 'all',
    searchKeyword: '',
    
    // 所有客户数据
    allClients: [],
    
    // 今日待跟进的客户
    todayFollowups: [],
    
    // 最近新增的客户
    recentClients: [],
    
    // 筛选后的客户列表
    filteredClients: []
  },
  
  onLoad: function() {
    // 更新当前时间
    this.updateCurrentTime();
    
    // 获取客户数据
    this.fetchClientsData();
  },
  
  // 更新当前时间
  updateCurrentTime: function() {
    const date = new Date();
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');
    this.setData({
      currentTime: `${hours}:${minutes}`
    });
  },
  
  // 获取客户数据
  fetchClientsData: function() {
    // 这里应该调用实际的API获取客户数据
    // 暂时使用模拟数据
    const mockClients = [
      {
        id: '001',
        name: '周小姐',
        phone: '138****1234',
        weddingDate: '2024-10-15',
        status: 'new',
        statusText: '新客资',
        tags: [
          { name: '高意向', isHigh: true },
          { name: '官网', isHigh: false }
        ],
        createTime: '2024-03-28',
        needFollowup: true
      },
      {
        id: '002',
        name: '李先生 & 王小姐',
        phone: '139****5678',
        weddingDate: '2024-12-25',
        status: 'visited',
        statusText: '已到店',
        tags: [
          { name: '小程序', isHigh: false }
        ],
        createTime: '2024-03-27',
        needFollowup: true
      },
      {
        id: '003',
        name: '陈小姐',
        phone: '186****9012',
        weddingDate: '2024-09-18',
        status: 'visited',
        statusText: '已到店',
        tags: [
          { name: '高意向', isHigh: true },
          { name: '转介绍', isHigh: false }
        ],
        createTime: '2024-03-26',
        needFollowup: true
      },
      {
        id: '004',
        name: '张女士',
        phone: '135****3456',
        weddingDate: '2025-03-08',
        status: 'new',
        statusText: '新客资',
        tags: [
          { name: '婚博会', isHigh: false }
        ],
        createTime: '2024-03-25',
        needFollowup: false
      },
      {
        id: '005',
        name: '赵先生 & 钱小姐',
        phone: '151****7890',
        weddingDate: '2024-05-20',
        status: 'deal',
        statusText: '已成交',
        tags: [
          { name: '小程序', isHigh: false }
        ],
        createTime: '2024-03-24',
        needFollowup: false
      }
    ];
    
    // 将数据分类
    const todayFollowups = mockClients.filter(client => client.needFollowup);
    const recentClients = mockClients.filter(client => !client.needFollowup);
    
    this.setData({
      allClients: mockClients,
      todayFollowups: todayFollowups,
      recentClients: recentClients,
      filteredClients: mockClients
    });
  },
  
  // 切换筛选项
  switchFilter: function(e) {
    const filter = e.currentTarget.dataset.filter;
    this.setData({ currentFilter: filter });
    
    // 根据筛选条件更新客户列表
    this.filterClients();
  },
  
  // 搜索输入
  searchInput: function(e) {
    this.setData({ searchKeyword: e.detail.value });
    
    // 根据搜索关键词更新客户列表
    this.filterClients();
  },
  
  // 筛选客户列表
  filterClients: function() {
    const { allClients, currentFilter, searchKeyword } = this.data;
    
    // 先根据筛选条件筛选
    let filtered = [...allClients];
    
    if (currentFilter !== 'all') {
      // 根据不同筛选条件筛选
      switch(currentFilter) {
        case 'new':
          filtered = filtered.filter(client => client.status === 'new');
          break;
        case 'visited':
          filtered = filtered.filter(client => client.status === 'visited');
          break;
        case 'deal':
          filtered = filtered.filter(client => client.status === 'deal');
          break;
        case 'following':
          filtered = filtered.filter(client => client.needFollowup);
          break;
        case 'high':
          filtered = filtered.filter(client => 
            client.tags.some(tag => tag.isHigh)
          );
          break;
      }
    }
    
    // 再根据搜索关键词筛选
    if (searchKeyword) {
      filtered = filtered.filter(client => 
        client.name.includes(searchKeyword) || 
        client.phone.includes(searchKeyword) ||
        client.tags.some(tag => tag.name.includes(searchKeyword))
      );
    }
    
    // 根据筛选结果更新分类
    const todayFollowups = filtered.filter(client => client.needFollowup);
    const recentClients = filtered.filter(client => !client.needFollowup);
    
    this.setData({
      filteredClients: filtered,
      todayFollowups: todayFollowups,
      recentClients: recentClients
    });
  },
  
  // 跳转到客户详情页
  navigateToDetail: function(e) {
    const clientId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/client_detail/client_detail?id=${clientId}`
    });
  },
  
  // 拨打电话
  callClient: function(e) {
    const phone = e.currentTarget.dataset.phone;
    // 将显示的电话号码替换为真实号码（去除*）
    const realPhone = phone.replace(/\*/g, '0');
    
    wx.makePhoneCall({
      phoneNumber: realPhone,
      fail(err) {
        console.log('拨打电话失败', err);
      }
    });
  },
  
  // 添加新客户
  addNewClient: function() {
    wx.navigateTo({
      url: '/pages/client_detail/client_detail?mode=add'
    });
  }
}) 